package 课设;

import java.util.ArrayList;
import java.util.Scanner;

import static 课设.StudentApp.students;

public class StudentManage {
    private static Scanner sc=new Scanner(System.in);
    public static int getMenu(){
        System.out.println("-----------学生成绩管理系统---------------");
        System.out.println("---------1.输入学生相关信息---------------");
        System.out.println("---------2.按平均分降序排列---------------");
        System.out.println("---------3.显示所有成绩-------------------");
        System.out.println("---------4.输入查询个人成绩---------------");
        System.out.println("---------5.显示各科平均分，最高分，最低分--");
        System.out.println("---------6.退出成绩管理系统---------------");
        System.out.println("-----------学生成绩管理系统---------------");
        System.out.println("请选择菜单：");
        int select=StudentManage.sc.nextInt();
        return select;
    }
    public void addStudent(ArrayList<Student>students){
        Student student=new Student();
        System.out.println("请输入学生的学号");
        student.setId((int) sc.nextLong());
        System.out.println("请输入学生的姓名");
        student.setName(sc.next());
        System.out.println("请输入学生的应用数学成绩");
        student.setMath(sc.nextDouble());
        System.out.println("请输入学生的大学英语成绩");
        student.setEnglish(sc.nextDouble());
        System.out.println("请输入学生的java程序设计成绩");
        student.setJava(sc.nextDouble());
        System.out.println("请输入学生的计算机应用基础成绩");
        student.setComputer(sc.nextDouble());
        double avg = student.getAvg(student);
        student.setAverage(avg);
        students.add(student);
        System.out.println("添加成功");
    }

    public void searchStudent(ArrayList<Student> students){
        if (students.size() == 0) {
            System.out.println("当前无数据，请添加后重试");
        } else {
            System.out.println("请输入要查找学生的学号：");
            long id = sc.nextLong();
            int flag = -1;
            Student student = new Student();
            //        查找对应学号，找到就退出
            for (int i = 0; i < students.size(); i++) {
                student = students.get(i);
                if (student.getId() == id) {
                    flag = i;
                    break;
                }
            }

            if (flag == -1) {
                System.out.println("未找到对应学号的学生，请确认后重新输入！");
            } else {
                System.out.println("对应学号的学生成绩如下：");
                System.out.println("学号\t 姓名\t  数学\t  英语\t  Java\t 计算机\t 平均分");
                System.out.format("%d       %s     %.2f   %.2f   %.2f   %.2f   %.2f\n", student.getId(), student.getName(), student.getMath(), student.getEnglish(), student.getJava(),student.getComputer(), student.getAvg(student));
                System.out.println("查找信息成功！");
            }
        }
    }
    public void sortStudent(ArrayList<Student> students){
        if (students.isEmpty()) {
            System.out.println("当前无数据，请添加后重试");
        } else {
            for (int i = 0; i < students.size() - 1; i++) {
                if (students.get(i).getAvg(students.get(i)) < students.get(i + 1).getAvg(students.get(i + 1))) {
                    Student tmp = students.get(i);
                    students.set(i, students.get(i + 1));
                    students.set(i + 1, tmp);
                }
            }

            System.out.println("排序后的学生成绩");
            System.out.println("学号\t 姓名\t  数学\t  英语\t  Java\t 计算机\t 平均分");
            for (Student student : students) {
                System.out.format("%d       %s     %.2f   %.2f   %.2f   %.2f   %.2f\n", student.getId(), student.getName(), student.getMath(), student.getEnglish(), student.getJava(),student.getComputer(), student.getAvg(student));
            }
        }
    }
    //显示所有成绩
    public void showAllStudent(ArrayList<Student> students){
        if (students.isEmpty()) {
            System.out.println("当前无数据，请先添加数据");
        } else {
            System.out.println("----------所有学生成绩如下----------");
            System.out.println("学号\t 姓名\t  数学\t  英语\t  Java\t 计算机\t 平均分");
            for (Student student : students) {
                System.out.format("%d       %s     %.2f   %.2f   %.2f   %.2f   %.2f\n", student.getId(), student.getName(), student.getMath(), student.getEnglish(), student.getJava(),student.getComputer(), student.getAvg(student));
            }
        }
    }

    public void averageScore(ArrayList<Student> students){
        int n = students.size();
        double max1 = 0;
        double min1 = students.get(0).getMath();;
        double sum1 = 0;
        for (int i = 0; i < n; i++) {
            sum1 = sum1 + students.get(i).getMath();
            if (students.get(i).getMath() > max1) {
                max1 = students.get(i).getMath();
            }
            if (students.get(i).getMath() < min1) {
                min1 = students.get(i).getMath();
            }
        }
        System.out.println("应用数学的平均成绩="+sum1/n+" 最高成绩="+max1+" 最低成绩="+min1);

        double max2 = 0;
        double min2 = students.get(0).getEnglish();;
        double sum2 = 0;
        for (int i = 0; i < n; i++) {
            sum2 = sum2 + students.get(i).getEnglish();
            if (students.get(i).getEnglish() > max2) {
                max2 = students.get(i).getEnglish();
            }
            if (students.get(i).getEnglish() <= min2) {
                min2 = students.get(i).getEnglish();
            }
        }
        System.out.println("大学英语的平均成绩="+sum2/n+" 最高成绩="+max2+" 最低成绩="+min2);

        double max3 = 0;
        double min3 = students.get(0).getJava();;
        double sum3 = 0;
        for (int i = 0; i < n; i++) {
            sum3 = sum3 + students.get(i).getJava();
            if (students.get(i).getJava() > max3) {
                max3 = students.get(i).getJava();
            }
            if (students.get(i).getJava() <= min3) {
                min3 = students.get(i).getJava();
            }
        }
        System.out.println("Java的平均成绩="+sum3/n+" 最高成绩="+max3+" 最低成绩="+min3);

        double max4 = 0;
        double min4 = students.get(0).getComputer();;
        double sum4 = 0;
        for (int i = 0; i < n; i++) {
            sum4 = sum4 + students.get(i).getComputer();
            if (students.get(i).getEnglish() > max4) {
                max4 = students.get(i).getComputer();
            }
            if (students.get(i).getComputer() <= min4) {
                min4 = students.get(i).getComputer();
            }
        }
        System.out.println("计算机应用基础的平均成绩="+sum4/n+" 最高成绩="+max4+" 最低成绩="+min4);
    }
}

